package cn.wgx.module.sys.web;


import cn.wgx.common.base.IBaseController;
import cn.wgx.common.entity.sys.Role;
import cn.wgx.common.entity.sys.RolePermission;
import cn.wgx.common.entity.sys.UserRole;
import cn.wgx.common.security.util.SysUserUtil;
import cn.wgx.common.util.ResponseDataUtil;
import cn.wgx.module.sys.service.PermissionService;
import cn.wgx.module.sys.service.RolePermissionService;
import cn.wgx.module.sys.service.RoleService;
import cn.wgx.module.sys.service.UserRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Set;

@Api(tags = "2.0 角色管理")
@RestController
@RequestMapping("/api/role")
public class RoleCtrl extends IBaseController<RoleService, Role> {

    @Autowired
    private RolePermissionService rolePermissionService;
    @Autowired
    private PermissionService permissionService;
    @Autowired
    private UserRoleService userRoleService;

    @ApiOperation("角色授权 -> 添加/删除权限, 一对多")
    @PatchMapping("updateRolePermission")
    public Map updateRolePermission(@ApiParam(value = "RolePermission对象数组,只需role_id和permission_id") @RequestBody List<RolePermission> rolePermissions, @RequestParam String access_token) {

        if (rolePermissions == null || rolePermissions.size() == 0) {
            return ResponseDataUtil.returnErrorMap("参数错误");
        }
        return ResponseDataUtil.returnData(rolePermissionService.updateRolePermission(rolePermissions, access_token));
    }

    @ApiOperation("根据用户ID获取角色列表（一个用户可以有多个角色）")
    @GetMapping("getRoleByUser")
    public Map getRoles(@RequestParam Integer user_id) {
        return ResponseDataUtil.returnData(service.findRolesByUserId(user_id));
    }

    @ApiOperation("根据角色ID获取权限列表（支持多个角色查询）")
    @GetMapping("getPerByRoles")
    public Map getPers(@RequestParam Set<Long> roleIds) {
        return ResponseDataUtil.returnData(permissionService.findByRoleIds(roleIds));
    }

    @ApiOperation(value = "修改用户角色", notes = "添加、删除用户角色，一对多")
    @PatchMapping("updateRoles")
    public Map updateRoles(@ApiParam(value = "UserRole对象数组,只需role_id和user_id") @RequestBody List<UserRole> userRoles, @RequestParam String access_token) {
        if (userRoles == null || userRoles.size() == 0) {
            return ResponseDataUtil.returnErrorMap("参数错误");
        }
        if (!SysUserUtil.getLoginAppUser(access_token).getUsername().equals("admin")) {
            return ResponseDataUtil.returnErrorMap("只有admin可以更新用户角色.");
        }
        return ResponseDataUtil.returnData(userRoleService.updateRoles(userRoles, access_token));
    }


    @ApiOperation(value = "根据主键ID删除数据")
    @DeleteMapping("del/{id}")
    public Map del(@PathVariable Integer id) {
        if(id.equals(1) || id.equals(2)){
            return ResponseDataUtil.returnErrorMap("默认角色不可删除");
        }
        return ResponseDataUtil.returnData(service.removeById(id));
    }

    @ApiOperation(value = "根据主键ID删除数据-批量")
    @DeleteMapping("delBatch")
    public Map delBatch(@RequestBody List<Integer> ids) {
        for(Integer id: ids){
            if(id.equals(1) || id.equals(2)){
                return ResponseDataUtil.returnErrorMap("默认角色不可删除");
            }
        }
        return ResponseDataUtil.returnData(service.removeByIds(ids));
    }
}
